//
//  FirstViewController.m
//  CRTX
//
//  Created by admin on 15/7/15.
//  Copyright (c) 2015年 北京淘伟科技有限公司. All rights reserved.
//

#import "FirstViewController.h"
#import "AppDelegate.h"

@interface FirstViewController ()<UIScrollViewDelegate>
{
    UIPageControl  *_pageControl;
    UIScrollView   *_scrollView;
    
}
@end

@implementation FirstViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self hideNavBar:YES];
    
    //创建scrollview
    _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SIZEWIDTH, SIZEHEIGHT)];
    //设置滚动区域
    [self.view addSubview:_scrollView];
    
    UIImageView * view1= [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SIZEWIDTH, SIZEHEIGHT)];
    view1.image = [UIImage imageNamed:@"引导页1.png"];
    [_scrollView addSubview:view1];
    
    UIImageView * view2= [[UIImageView alloc]initWithFrame:CGRectMake(SIZEWIDTH, 0, SIZEWIDTH, SIZEHEIGHT)];
    view2.image = [UIImage imageNamed:@"引导页2.png"];
    [_scrollView addSubview:view2];
    
//    UIImageView * view3= [[UIImageView alloc]initWithFrame:CGRectMake(SIZEWIDTH * 2, 0, SIZEWIDTH, SIZEHEIGHT)];
//    view3.image = [UIImage imageNamed:@"guide03.png"];
//    [_scrollView addSubview:view3];
    
    UIImageView * view4= [[UIImageView alloc]initWithFrame:CGRectMake(SIZEWIDTH * 2, 0, SIZEWIDTH, SIZEHEIGHT)];
    view4.image = [UIImage imageNamed:@"引导页3.png"];
    [_scrollView addSubview:view4];
    view4.userInteractionEnabled = YES;
    
    //设置滚动区域
    _scrollView.contentSize =CGSizeMake(SIZEWIDTH * 3, SIZEHEIGHT);
    //设置分页效果
    _scrollView.pagingEnabled = YES;
    
    _scrollView.scrollEnabled= YES;
//    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.delegate = self;
    
    
    _pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake((SIZEWIDTH-SIZEWIDTH/3)/2, SIZEHEIGHT - 40, SIZEWIDTH/3, 20)];
//    [self.view addSubview:_pageControl];
    _pageControl.numberOfPages = 3;
    _pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
    _pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
    [_pageControl addTarget:self action:@selector(handlePageControlChangeAction:) forControlEvents:UIControlEventValueChanged];
    
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapUIImageViewAction:)];
    [view4 addGestureRecognizer:tapGestureRecognizer];
    
    [self.view addSubview:_pageControl];
}


-(void)tapUIImageViewAction:(UITapGestureRecognizer*)tap
{

    AppDelegate * delegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
    

    
    [delegate joinTabBar];
    
}

//pagecontrol的响应方法
- (void)handlePageControlChangeAction:(id)sender
{
    
    CGFloat offsetX = _scrollView.bounds.size.width * _pageControl.currentPage;
    
    //根据pagecontrol的当前页数，设置scollview的controffset，显示对应的页面
    [_scrollView setContentOffset:CGPointMake(offsetX, 0) animated:YES];
}
//结束减速时触发(停⽌止时)
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    //计算当前第几页
    CGPoint offset= scrollView.contentOffset;
    NSInteger pageNumber = offset.x /scrollView.bounds.size.width;
    _pageControl.currentPage = pageNumber;
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
